<?php

function sso_authentication_load_profile()
{
	$data =  Event::$data;
	$userProfile = $data['profile'];
	// Pobierz z bazy danych dane identyfikacyjne użytkownika
	$db = Database::instance();
	$extraProfileInfo = $db->query("SELECT
	u.prawa,f.typ
	FROM
		users AS u
		JOIN firma AS f USING(id_firma)
	WHERE
		 u.login = ? AND u.email = ?
	",array($userProfile['user_login'], $userProfile['user_email']))->result(false)->current();
	
	$finalProfile = array_merge($userProfile, $extraProfileInfo);
	$user = BasicUser::instance();
	$user->set('profile', $finalProfile);
	$user->setAuthenticated(true);	
	$user->addCredential($finalProfile['prawa']);
}

Event::add('logintrade_sso.user_authenticated','sso_authentication_load_profile');